var showMenu = false;
function menu(event) {
  if (showMenu) {
    $('#hover').hide();
    $('#body').removeClass('hidebody');
    $('#menuIcon').attr('src', '/assets/images/menu.png');
    $('body').off('touchmove');
    showMenu = false;
  } else {
    $('#hover').show();
    $('#body').addClass('hidebody');
    $('#menuIcon').attr('src', '/assets/images/cancel.png');
    $('body').on('touchmove', function (event) {
      event.preventDefault();
    });
    showMenu = true;
  }
}

function scrollToTool (className) {
  $('html,body').animate({scrollTop:$('.' + className).offset().top}, 800);
}

function getQueryString(name) { 
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
  var r = window.location.search.substr(1).match(reg); 
  if (r != null) return unescape(r[2]); 
  return null; 
} 

function createHtml(_data, _id) {
  var _html = '';
  for (var i = 0; i< _data.length;i++) {
    _html += '<a href="/product-detail.html?id=' + _data[i].product_id + '" class="thumbs" style="position: relative;">' + 
    '<img style="width: 100%;height: 100%;" src="http://webapi.isuuny.xyz/resource/getImage?filename=' + _data[i].product_cover + '">' +
    '<span>' + _data[i].product_name + ' </span>' +
    '</a>';
  }
  $('#' + _id).html(_html);
}

function createNewsHtml(_data, _id) {
  var _html = '';
  for (var i = 0; i< _data.length; i++) {
    let date = formatDate(_data[i].news_create_date)
    _html += '<a href="/news-detail.html?id=' + _data[i].news_id + '" class="news"><div class="news-left"><div class="dateDiv"><span class="year">' + date.year + ' - ' + date.month + '</span><span class="Day">' + date.day + '</span></div></div><div class="news-right"><span>' + 
    _data[i].news_title + '</span><p>' + _data[i].news_content + '</p></div></a>'
  }
  $('#' + _id).html(_html);
}

function formatDate(date) {
  let formats = date.replace('Z', '').replace('.000', '').split('T')
  return {
    year: formats[0].substring(0, 4),
    month: getMonthEng(formats[0].substring(5, 7)),
    day: formats[0].substring(8, 10)
  }
}

function formatDate2(date) {
  return date.replace('Z', '').replace('.000', '').replace('T',' ')
}

function getMonthEng(month) {
  switch(month) {
    case '01':return 'Jan.';
    case '02':return 'Feb.';
    case '03':return 'Mar.';
    case '04':return 'Apr.';
    case '05':return 'May.';
    case '06':return 'Jun.';
    case '07':return 'Jul.';
    case '08':return 'Aug.';
    case '09':return 'Sep.';
    case '10':return 'Oct.';
    case '11':return 'Nov.';
    case '12':return 'Dec.';
    default: return ''
  }
}

function createNewsDetailHtml(_data, _id) {
  if (_data.length > 0) {
    var _html = '';
    _html += '<h1 class="news-detail-title">' + _data[0].news_title + '</h1><div class="news-detail-info"><span style="display: inline-block;height: 3.6rem;line-height: 3.6rem;font-size: 1.8rem;margin-left: 8rem;">Published: ' + 
    formatDate2(_data[0].news_create_date) + '</span></div><p>' + _data[0].news_content + '</p>'
    $('#' + _id).html(_html);
  }
}

function searchProduct(_input) {
  var keyword = document.getElementById(_input).value.toLowerCase();
  window.location.href = '/product-list.html?keyword=' + keyword;
}

function bindSearchEnter() {
  $("#search_txtbox").keydown(function () {
    if (event.keyCode == "13") {//keyCode=13是回车键
        $('#search-button').click();
    }
});
}